These problems are offered to help understand the lecture material and assist with completing homeworks; they will also form the basis for some problems on the midterms. They are entirely optional, and feel free to skip if you understand a concept!
You should do these problems on paper or mentally first, and only THEN check the solution.
This set of problems uses Bare-Bones Haskell, with explicit data declarations for Pairs and Lists; here is a version of the same problems with the built-in Haskell notations for tuples and lists.
2. BareBones Haskell: Types and type checking
Consider the following data declaration:
data D a = A Integer | B Bool | C a data Nat = Zero | Succ Nat data Pair a b = P a b data List a = Nil | Cons a (List a) data Tree a = Null | Node (Tree a) a (Tree a)
For the first part of these exercises, we will ask you to give the types of various constructors in the above data declarations; then we will define some functions using these data, plus Integer and Bool, and you will need to give the types of the functions, as well as some expressions involving these functions.
We assume that we can have Integer and Bools as defined in the Haskell Prelude. Assume for the purposes of these exercises that all arithmetic operators operate only on Integers, so pretend the type of (+) is:
(+): Integer -> Integer -> Integer.
If the expression is an error, explain where the error occurs.
Give the type of the following constructors.
2.1. Zero
2.2. Succ
2.3. B
2.4. C
2.5. Cons
2.6. P
2.7. Node
Give the type of the following expressions
2.8. (Succ Zero)
2.9. C 6
2.10. P 5 True
2.12. C (P 4 (A 5))
2.12. C (C (B True))
2.13. P (P 9 (Succ Zero)) (P (C Zero) True)
2.14. (Cons (C 4) (Cons (A 5) Nil))
2.15. (Cons (Cons True Nil) Nil)
2.16. Node Null (Cons (Succ (Succ Zero)) Nil) Null
2.17. C (Node Null (Cons (C (P 3 Zero)) Nil) Null)
Suppose for this section that in addition to the data declarations above, we have the following data and function definitions. (The types Either and Maybe are defined in the Prelude and used very often in Haskell programming.)
data Either a b = Left a | Right b data Maybe a = Nothing | Just a safeHead Nil = Nothing safeHead (Cons x _) = Just x safeTail Nil = Nothing safeTail (Cons _ xs) = Just xs fst (P x _) = x snd (P _ y) = y f (P x y) = x + y g (P True x) = Left x g (P False y) = Right y h Zero = 0 h (Succ x) = 1 + (h x)For the next seven problems, just give the types of the functions just defined. The last four will be simple polymorphic types.
2.18. f
2.19. g
2.20. h
:
2.21. safeHead
2.22. safeTail
2.23. fst
2.24. snd
Give the types of the following expressions. If the expression can not be typed, explain why.
2.25. g (P (not True) (f (P 4 5)))
2.26. fst (P (h Zero) (g (P True True))))
2.27. safeHead (Cons (fst (P 3 True)) (Cons (h Zero) Nil))
2.28. safeHead (safeTail (Cons (fst (P 3 True)) (Cons (h Zero) Nil)))
2.29.
g (P (fst x) (snd x)) where x = (P (snd (P 3 True)) (P False 5))
2.30.
g (P (snd x) (fst x)) where x = (P (snd (P 3 True)) (P False 5))
2.31.
g (P (fst x) (fst x)) where x = (P (snd (P 3 True)) (P False 5))